package com.tebiecloud.mpweixin.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinMggFlow;
import com.tebiecloud.mpweixin.server.service.MpweixinMggFlowService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 用户乖乖币记录表 前端控制器
 * @author tebie
 * @date 2019-09-16
 */
@RestController
@RequestMapping("/mpweixinmggflow")
public class MpweixinMggFlowController {

    @Autowired
    private MpweixinMggFlowService mpweixinMggFlowService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @PostMapping(value = "list")
    public ResultBody<IPage<MpweixinMggFlow>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        MpweixinMggFlow query = pageParams.mapToObject(MpweixinMggFlow.class);
        QueryWrapper<MpweixinMggFlow> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(mpweixinMggFlowService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<MpweixinMggFlow> get(@RequestParam("id") Long id) {
        MpweixinMggFlow entity = mpweixinMggFlowService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param userId 账号id,
     * @param orderId 关联的订单id,
     * @param transactionFunds 本次交易金额（单位：分）,
     * @param residueFunds 原有的资金数额,
     * @param futureFunds 交易后的资金数额,
     * @param transactionType 交易类型：1、核销获得乖乖币，2、支付消费乖乖币，3、关闭订单退返，4订单退款退返
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "userId", required = false) Long userId,
        @RequestParam(value = "orderId", required = false) Long orderId,
        @RequestParam(value = "transactionFunds", required = false) Integer transactionFunds,
        @RequestParam(value = "residueFunds", required = false) Integer residueFunds,
        @RequestParam(value = "futureFunds", required = false) Integer futureFunds,
        @RequestParam(value = "transactionType", required = false) Integer transactionType
    ) {
        MpweixinMggFlow entity = new MpweixinMggFlow();
        entity.setUserId(userId);
        entity.setOrderId(orderId);
        entity.setTransactionFunds(transactionFunds);
        entity.setResidueFunds(residueFunds);
        entity.setFutureFunds(futureFunds);
        entity.setTransactionType(transactionType);
        mpweixinMggFlowService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param userId 账号id
     * @param orderId 关联的订单id
     * @param transactionFunds 本次交易金额（单位：分）
     * @param residueFunds 原有的资金数额
     * @param futureFunds 交易后的资金数额
     * @param transactionType 交易类型：1、核销获得乖乖币，2、支付消费乖乖币，3、关闭订单退返，4订单退款退返
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "mpweixinMggFlowId", required = false) Long mpweixinMggFlowId,
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "orderId", required = false) Long orderId,
            @RequestParam(value = "transactionFunds", required = false) Integer transactionFunds,
            @RequestParam(value = "residueFunds", required = false) Integer residueFunds,
            @RequestParam(value = "futureFunds", required = false) Integer futureFunds,
            @RequestParam(value = "transactionType", required = false) Integer transactionType
    ) {
        MpweixinMggFlow entity = new MpweixinMggFlow();
        entity.setMpweixinMggFlowId(mpweixinMggFlowId);
        entity.setUserId(userId);
        entity.setOrderId(orderId);
        entity.setTransactionFunds(transactionFunds);
        entity.setResidueFunds(residueFunds);
        entity.setFutureFunds(futureFunds);
        entity.setTransactionType(transactionType);
        mpweixinMggFlowService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            mpweixinMggFlowService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            mpweixinMggFlowService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
